package learm.pers.studygitlab.code.controller;

import learm.pers.studygitlab.code.service.CodeService;
import learm.pers.studygitlab.code.utile.response.JsonResult;
import learm.pers.studygitlab.code.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName: CodeController
 * @Description:
 * @Author: fangqing
 * @Date: 2021/6/28
 */
@RestController
@RequestMapping(value="/code")
public class CodeController {

    @Autowired
    private CodeService codeService;

    /** *********************** start 用户 ***************************** **/
    /**
     * 创建用户（只有管理员才能进行创建）
     * @param gitUserVo
     * @return
     */
    @PostMapping(value="/createUser")
    public JsonResult createUser(@RequestBody GitUserVo gitUserVo){
        String mag = codeService.createUser(gitUserVo);
        if("1".equals(mag)){
            return JsonResult.createBySuccessMessage("创建成功!");
        }else{
            return JsonResult.createByFormValid("创建失败!");
        }
    }

    /**
     * 根据用户id获取用户
     * @param userId
     * @return
     */
    @GetMapping(value="/findUser")
    public JsonResult findUser(Long userId){
        GitUserVo user = codeService.findUser(userId,"0");
        if(!"".equals(user) && user!=null){
            return JsonResult.createBySuccess("获取用户成功", user);
        }else{
            return JsonResult.createByError("获取用户失败!");
        }
    }
    /**
     * 编辑用户（只有管理员才能进行）
     * @param gitUserVo
     * @return
     */
    @PostMapping(value="/editUser")
    public JsonResult editUser(@RequestBody GitUserVo gitUserVo){
        String mag = codeService.editUser(gitUserVo);
        if("1".equals(mag)){
            return JsonResult.createBySuccessMessage("编辑成功!");
        }else{
            return JsonResult.createByFormValid("编辑失败!");
        }
    }

    /**
     * 删除用户（只有管理员才能进行）
     * @param userId
     * @param project_Uuid
     * @return
     */
    @DeleteMapping(value="/deleteUser")
    public JsonResult deleteUser(@RequestParam("userId") Long userId, @RequestParam("project_Uuid") String project_Uuid){
        String mag = codeService.deleteUser(userId,project_Uuid);
        if("1".equals(mag)){
            return JsonResult.createBySuccessMessage("删除成功!");
        }else{
            return JsonResult.createByFormValid("删除失败!");
        }
    }

    /**
     * 创建用户个人令牌
     * @param gitPrivateTokenVo
     * @return
     */
    @PostMapping(value="/createUserPrivateToken")
    public JsonResult createUserPrivateToken(@RequestBody GitPrivateTokenVo gitPrivateTokenVo){
        String mag = codeService.createUserPrivateToken(gitPrivateTokenVo);
        if("1".equals(mag)){
            return JsonResult.createBySuccessMessage("创建成功!");
        }else if("2".equals(mag)){
            return JsonResult.createByFormValid("创建失败!");
        }else{
            return JsonResult.createByFormValid("该用户已生成过令牌!");
        }
    }


    /** *********************** end 用户 ***************************** **/

    /**
     * 查询已有代码库列表
     * @return
     */
    @GetMapping(value="/getLongRangeProjects")
    public JsonResult getLongRangeProjects(int pageIndex,int pageSize,String projectUuid,String codeName){
        List<GitProjectVo> longRangeProjects = codeService.getLongRangeProjects(pageIndex,pageSize,projectUuid,codeName);
        return JsonResult.createBySuccess(longRangeProjects);
    }


    /**
     * 根据代码库Id查询该代码库的详细信息
     * @param codeId 代码库Id
     * @return
     */
    @GetMapping(value="/getProjectsByCodeId")
    public JsonResult getProjectsByCodeId(Long codeId){
        GitProjectVo projects = codeService.getProjectsByCodeId(codeId);
        return JsonResult.createBySuccess(projects);
    }

    /**
     * 创建代码库
     * @param gitProjectAddVo
     * @return
     */
    @PostMapping(value="/addCodeBase")
    public JsonResult addCodeLib(@RequestBody GitProjectAddVo gitProjectAddVo){
        String uuid = codeService.addCodeLib(gitProjectAddVo);
        if("".equals(uuid) || uuid==null){
            return JsonResult.createByFormValid("创建失败!");
        }else{
            return JsonResult.createBySuccessMessage("创建成功!");

        }
    }


    /**
     * 通过压缩包导入代码库
     * @param file 文件
     * @param name 要导入的项目的名称。如果未提供，则默认为项目路径
     * @param path 新项目的名称和路径
     * @param namespace 要将项目导入到的命名空间的 ID 或路径。默认为当前用户的命名空间
     * @param projectId
     * @return
     */
    @PostMapping(value="/importCodeBase")
    public JsonResult importCodeBase(@RequestParam(value = "file", required = false) MultipartFile file, String name, String path, Long namespace, String projectId){
        Boolean bool = codeService.importCodeBase(file, name, path, namespace, projectId);
        if(!bool){
            return JsonResult.createByFormValid("导入失败！");
        }else{
            return JsonResult.createBySuccessMessage("导入成功");
        }
    }

    /**
     * 通过url导入代码库
     * @param gitProjectRepoByUrlVo
     * @return
     */
    @PostMapping(value="/importRepoByUrlCodeBase")
    public JsonResult importRepoByUrlCodeBase(@RequestBody GitProjectRepoByUrlVo gitProjectRepoByUrlVo){
        String flag = codeService.importRepoByUrlCodeBase(gitProjectRepoByUrlVo);
        if("1".equals(flag)){
            return JsonResult.createBySuccessMessage("导入成功");
        }else if("2".equals(flag)){
            return JsonResult.createByFormValid("导入失败！");
        }else if("3".equals(flag)){
            return JsonResult.createByFormValid("导入失败,请检查导入的URL是否正确！");
        }
        return JsonResult.createByFormValid("导入失败！");
    }

    /**
     * 编辑代码库
     * @param gitProjectAddVo
     * @return
     */
    @PostMapping(value="/editCodeBase")
    public JsonResult editCodeBase(@RequestBody GitProjectAddVo gitProjectAddVo){
        String flag = codeService.editCodeBase(gitProjectAddVo);
        if("yes".equals(flag)){
            return JsonResult.createBySuccessMessage("修改成功!");
        }else{
            return JsonResult.createByFormValid("修改失败！");

        }
    }

    /**
     * 删除代码库
     * @param codeId 代码库id
     * @param projectId 项目id
     * @return
     */
    @DeleteMapping(value="/deleteCodeLib")
    public JsonResult deleteCodeLib(@RequestParam("codeId") Long codeId, @RequestParam("projectId") String projectId){
        codeService.deleteCodeLib(codeId,projectId);
        return JsonResult.createBySuccess("删除成功");
    }

    /**
     * 获取群组
     * @param id
     * @param pageIndex
     * @param pageSize
     * @param isPage 是否分页，YES:需要分页  NO:不需要分页
     * @return
     */
    @GetMapping(value="/findGroups")
    public JsonResult findGroups(Long id, int pageIndex,int pageSize, String isPage){
        Object namespaces = codeService.findGroups(id,pageIndex,pageSize,isPage);
        return JsonResult.createBySuccess("查询成功",namespaces);
    }

    /**
     * 添加群组
     * @param gitGroupsVo
     * @return
     */
    @PostMapping(value="/addGroups")
    public JsonResult addGroups(@RequestBody GitGroupsVo gitGroupsVo){
        String flag = codeService.addGroups(gitGroupsVo);
        if("1".equals(flag)){
            return JsonResult.createBySuccessMessage("创建成功！");
        }else if("2".equals(flag)){
            return JsonResult.createByFormValid("创建失败！");
        }else if("3".equals(flag)){
            return JsonResult.createByFormValid("该群组已存在，请更换群组名称！");
        }
        return JsonResult.createByFormValid("导入失败！");

    }

    /**
     * 导入群组
     * @param file 文件
     * @param name 要导入的群组的名称。如果未提供，则默认为项目路径
     * @param path 新群组的名称和路径
     * @return
     */
    @PostMapping(value="/importGroups")
    public JsonResult importGroups(@RequestParam(value = "file", required = false) MultipartFile file, String name, String path, Long namespace, String projectId){
        Boolean bool = codeService.importGroups(file, name, path);
        if(!bool){
            return JsonResult.createByFormValid("导入失败！");
        }else{
            return JsonResult.createBySuccessMessage("导入成功！");
        }
    }

    /**
     * 编辑群组
     * @param gitGroupsVo
     * @return
     */
    @PostMapping(value="/editGroups")
    public JsonResult editGroups(@RequestBody GitGroupsVo gitGroupsVo){
        String flag = codeService.editGroups(gitGroupsVo);
        if("yes".equals(flag)){
            return JsonResult.createBySuccessMessage("修改成功！");
        }else{
            return JsonResult.createByFormValid("修改失败！");

        }
    }

    /**
     * 删除群组 gitLab默认延迟删除 7天 现在已改为立即删除
     * @param groupsId
     * @return
     */
    @DeleteMapping(value="/deleteGroups")
    public JsonResult deleteGroups(@RequestParam("groupsId") Long groupsId) throws InterruptedException {
        String msg = codeService.deleteGroups(groupsId);
        if("yes".equals(msg)){
            Thread.currentThread().sleep(1000);
            return JsonResult.createBySuccessMessage("删除成功，同时删除该组下的代码库！");
        }else{
            return JsonResult.createByFormValid("删除失败！");
        }
    }

    /**
     * 根据群组获取群组的代码库
     * @param pageIndex
     * @param pageSize
     * @param groupsId
     * @return
     */
    @GetMapping(value="/findCodeByGroupsId")
    public JsonResult findCodeByGroupsId(int pageIndex, int pageSize, Long groupsId){
        List<GitProjectVo> list = codeService.findCodeByGroupsId(pageIndex,pageSize,groupsId,"0");
        return JsonResult.createBySuccess("获取列表成功", list);
    }

    /**
     * 根据代码库id和 状态 获取合并请求
     * @param codeId 代码库id
     * @param state  ( opened、closed、 或merged) 或所有状态 ( ) 的合并请求all
     * @return
     */
    @GetMapping(value="/getMerge")
    public JsonResult getMerge(Long codeId,String state){
        List<GitMergeVo> list = codeService.getMerge(codeId, state);
        return JsonResult.createBySuccess("查询成功",list);
    }

    /**
     * 根据代码库id，获取分支，也可获取该代码库的单个分支
     * @param codeId
     * @param branchName
     * @return
     */
    @GetMapping(value="/getBranches")
    public JsonResult getBranches(Long codeId,String branchName){
        Object branches = codeService.getBranches(codeId, branchName);
        return JsonResult.createBySuccess("查询成功",branches);
    }

    /**
     * 添加分支
     * @param gitBranchAddVo
     * @return
     */
    @PostMapping(value="/addBranches")
    public JsonResult addBranches(@RequestBody GitBranchAddVo gitBranchAddVo){
        GitBranchVo gitBranchVo = codeService.addBranches(gitBranchAddVo);
        if(!"".equals(gitBranchVo) && gitBranchVo != null){
            return JsonResult.createBySuccessMessage("添加成功！");
        }else{
            return JsonResult.createByFormValid("添加失败,请检查是否存在该分支！");
        }
    }

    /**
     * 根据项目及分支获取提交记录
     * @param codeId 代码库id
     * @param branchName 分支名称
     * @param pageIndex 页 从1开始
     * @return
     */
    @GetMapping(value="/getBranchesCommits")
    public JsonResult getBranchesCommits(Long codeId,String branchName,int pageIndex,int pageSize) throws Exception {
        List<GitBranchCommitVo> list= codeService.getBranchesCommits(codeId, branchName,pageIndex,pageSize);
        return JsonResult.createBySuccess("查询成功",list);
    }



}
